﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.IO;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;

using JRCore.protocol;
using JRCore.enums;

using DataRecv.tools;
using DataRecv.enums;
using DataRecv.models.device;
using DataRecv.models.tail;
using YoSunSDK;

namespace DataRecv.models.recever
{
    public class Worker
    {
        private const int cHeight = 20;
        private const long cQty = 50;//压缩比
        private static EncoderParameters m_eps;//编码参数数组
        public static SolidBrush m_txtbrush;
        public static SolidBrush m_backBrish;
        public static Font m_txtfont;
        public Worker()
        {
            m_eps = new EncoderParameters(1);
            m_eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, cQty);
            m_txtbrush = new SolidBrush(Color.Black);
            m_backBrish = new SolidBrush(Color.White);
            m_txtfont = new Font("宋体", 12, FontStyle.Bold);
        }

        private static void DoWork(object obj)
        {
            Worker_Bag bag = (Worker_Bag)obj;
            
            DataRecvRecord record = bag.record;
            DeviceView thisview = bag.view;
            byte[] joinedpic = null;
            try
            {
                #region 1确定数据类型/违法类型
                Enum_CaptureSystem recordtype = Enum_CaptureSystem.NONE;
                Enum_VioType viotype = Enum_VioType.卡口;
                switch (record.P5)
                {
                    case "0":
                        recordtype = Enum_CaptureSystem.卡口; viotype = Enum_VioType.卡口;
                        break;
                    case "1":
                        recordtype = Enum_CaptureSystem.低速; viotype = Enum_VioType.低速;
                        break;
                    case "2":
                        recordtype = Enum_CaptureSystem.超速;
                        //超速判断
                        int speed = 0;
                        int speedlimit = 0;
                        try
                        {
                            speed = int.Parse(record.P27);
                            speedlimit = int.Parse(record.P26);
                        }
                        catch
                        {
                            speed = 0;
                            speedlimit = 0;
                            record.P27 = "0";
                            record.P26 = "0";
                        }
                        if ((speedlimit > 0) && (speed > speedlimit))
                        {
                            double rate = (double)(speed - speedlimit) / speedlimit;

                            if ((G.SystemParam.DJPicCheck) && (rate > 0) && (rate <= 0.1))
                            {
                                if (LogTool.gDebugEnable)
                                {
                                    LogTool.log.Info("超速10%过滤，超速比=" + rate.ToString());
                                }
                                return;
                            }

                            if (record.P22.Contains("蓝"))
                            {
                                if (rate > 0.9)
                                {
                                    viotype = Enum_VioType.蓝牌超速90;
                                }
                                else if ((rate > 0.7) && (rate <= 0.9))
                                {
                                    viotype = Enum_VioType.蓝牌超速70_90;
                                }
                                else if ((rate > 0.5) && (rate <= 0.7))
                                {
                                    viotype = Enum_VioType.蓝牌超速50_70;
                                }
                                else if ((rate > 0.2) && (rate <= 0.5))
                                {
                                    viotype = Enum_VioType.蓝牌超速20_50;
                                }
                                else if ((rate > 0.1) && (rate <= 0.2))
                                {
                                    viotype = Enum_VioType.蓝牌超速10_20;
                                }
                                else if ((rate > 0) && (rate <= 0.1))
                                {
                                    viotype = Enum_VioType.蓝牌超速10;
                                }

                            }
                            else if (record.P22.Contains("黄"))
                            {

                                if (rate > 0.9)
                                {
                                    viotype = Enum_VioType.黄牌超速90;
                                }
                                else if ((rate > 0.7) && (rate <= 0.9))
                                {
                                    viotype = Enum_VioType.黄牌超速70_90;
                                }
                                else if ((rate > 0.5) && (rate <= 0.7))
                                {
                                    viotype = Enum_VioType.黄牌超速50_70;
                                }
                                else if ((rate > 0.2) && (rate <= 0.5))
                                {
                                    viotype = Enum_VioType.黄牌超速20_50;
                                }
                                else if ((rate > 0.1) && (rate <= 0.2))
                                {
                                    viotype = Enum_VioType.黄牌超速10_20;
                                }
                                else if ((rate > 0) && (rate <= 0.1))
                                {
                                    viotype = Enum_VioType.黄牌超速10;
                                }

                            }
                        }
                        break;
                    case "3":
                        recordtype = Enum_CaptureSystem.逆行; viotype = Enum_VioType.逆行;
                        break;
                    case "4":
                        recordtype = Enum_CaptureSystem.闯红灯; viotype = Enum_VioType.闯红灯;
                        break;
                    case "5":
                        recordtype = Enum_CaptureSystem.压线; viotype = Enum_VioType.压线;
                        break;
                    case "6":
                        recordtype = Enum_CaptureSystem.不按导向行驶; viotype = Enum_VioType.不按导向行驶;
                        break;
                    case "7":
                        recordtype = Enum_CaptureSystem.路口滞留; viotype = Enum_VioType.路口滞留;
                        break;
                    case "8":
                        recordtype = Enum_CaptureSystem.机占非; viotype = Enum_VioType.机占非;
                        break;
                    case "9":
                        recordtype = Enum_CaptureSystem.违章变道; viotype = Enum_VioType.违章变道;
                        break;
                    case "10":
                        recordtype = Enum_CaptureSystem.占专用车道; viotype = Enum_VioType.占专用车道;
                        break;
                    case "11":
                        recordtype = Enum_CaptureSystem.黄牌禁限; viotype = Enum_VioType.黄牌禁限;
                        break;
                    case "12":
                        recordtype = Enum_CaptureSystem.违章停车; viotype = Enum_VioType.违章停车;
                        break;
                    default:
                        recordtype = Enum_CaptureSystem.卡口; viotype = Enum_VioType.卡口;
                        break;
                }
                bag.bz = viotype.ToString();
                if (LogTool.gDebugEnable) LogTool.log.Info("3 " + bag.Txt());
                record.P10 = ((VioParam)G.SystemParam.Wfxwdms[viotype]).viocode;//加载违法代码
                #endregion

                #region 2图片过滤

                if (thisview.deviceparam.workmode != (thisview.deviceparam.workmode | recordtype)) return;

                //整体过滤
                if ((recordtype != Enum_CaptureSystem.卡口) && (G.SystemParam.DJPlateFilterEnable))
                {
                    if ((record.P22 == "") && (G.SystemParam.DJPlateFilterType.Equals(G.SystemParam.DJPlateFilterType | Enum_PlateFilterType.无牌过滤))) return;
                    if ((record.P22.Contains("蓝")) && (G.SystemParam.DJPlateFilterType.Equals(G.SystemParam.DJPlateFilterType | Enum_PlateFilterType.蓝牌过滤))) return;
                    if ((record.P22.Contains("黄")) && (G.SystemParam.DJPlateFilterType.Equals(G.SystemParam.DJPlateFilterType | Enum_PlateFilterType.黄牌过滤))) return;
                }

                //BRT车辆
                if (G.SystemParam.BRTEnable == true)
                {
                    if ((record.P23 != "") && (G.BRTs != null))
                    {
                        if (G.BRTs.Contains(record.P23)) return;
                    }
                }



                //电警非合成图片缺失过滤
                if ((recordtype != Enum_CaptureSystem.卡口) && (record.P4 == "0"))
                {
                    //18140个3张图片不全的记录中，有14299个违法行为为"16252"的记录，90%的记录为闯红灯
                    //电警未合成时，如果场景图中不全，则不发送
                    int picCount = 0;
                    if (record.Pic1 != null) picCount++;
                    if (record.Pic2 != null) picCount++;
                    if (record.Pic3 != null) picCount++;
                    if (record.Pic4 != null) picCount++;

                    if (picCount < int.Parse(record.P3))
                    {
                        LogTool.log.Info(record.P15 + "电警缺失" + picCount.ToString() + "<" + record.P3);
                        return;
                    }
                }

                #endregion

                #region 2.5闯禁区筛查
                bool cjq = false;//闯禁区标志
                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    if (thisview.deviceparam.djsnapbytime == true)
                    {
                        bool found = false;
                        int hour = int.Parse(record.P18.Substring(8, 2));
                        int min = int.Parse(record.P18.Substring(10, 2));
                        int hour_start = int.Parse(thisview.deviceparam.time1_start.Substring(0, 2));
                        int min_start = int.Parse(thisview.deviceparam.time1_start.Substring(3, 2));
                        int hour_end = int.Parse(thisview.deviceparam.time1_end.Substring(0, 2));
                        int min_end = int.Parse(thisview.deviceparam.time1_end.Substring(3, 2));
                        if (((hour_start == hour_end) && (min_start < min_end)) || (hour_start < hour_end))//时段1有效
                        {
                            if ((((hour_start == hour) && (min_start <= min)) || (hour_start < hour)) && (((hour == hour_end) && (min <= min_end)) || (hour < hour_end)))
                            {
                                found = true;
                            }
                        }

                        hour_start = int.Parse(thisview.deviceparam.time2_start.Substring(0, 2));
                        min_start = int.Parse(thisview.deviceparam.time2_start.Substring(3, 2));
                        hour_end = int.Parse(thisview.deviceparam.time2_end.Substring(0, 2));
                        min_end = int.Parse(thisview.deviceparam.time2_end.Substring(3, 2));
                        if (((hour_start == hour_end) && (min_start < min_end)) || (hour_start < hour_end))//时段2有效
                        {
                            if ((((hour_start == hour) && (min_start <= min)) || (hour_start < hour)) && (((hour == hour_end) && (min <= min_end)) || (hour < hour_end)))
                            {
                                found = true;
                            }
                        }

                        hour_start = int.Parse(thisview.deviceparam.time3_start.Substring(0, 2));
                        min_start = int.Parse(thisview.deviceparam.time3_start.Substring(3, 2));
                        hour_end = int.Parse(thisview.deviceparam.time3_end.Substring(0, 2));
                        min_end = int.Parse(thisview.deviceparam.time3_end.Substring(3, 2));
                        if (((hour_start == hour_end) && (min_start < min_end)) || (hour_start < hour_end))//时段3有效
                        {
                            if ((((hour_start == hour) && (min_start <= min)) || (hour_start < hour)) && (((hour == hour_end) && (min <= min_end)) || (hour < hour_end)))
                            {
                                found = true;
                            }
                        }

                        hour_start = int.Parse(thisview.deviceparam.time4_start.Substring(0, 2));
                        min_start = int.Parse(thisview.deviceparam.time4_start.Substring(3, 2));
                        hour_end = int.Parse(thisview.deviceparam.time4_end.Substring(0, 2));
                        min_end = int.Parse(thisview.deviceparam.time4_end.Substring(3, 2));
                        if (((hour_start == hour_end) && (min_start < min_end)) || (hour_start < hour_end))//时段4有效
                        {
                            if ((((hour_start == hour) && (min_start <= min)) || (hour_start < hour)) && (((hour == hour_end) && (min <= min_end)) || (hour < hour_end)))
                            {
                                found = true;
                            }
                        }

                        if (found == true)
                        {
                            if (thisview.deviceparam.djsnapbyplate == true)
                            {
                                if ((record.P22 == "") && (thisview.deviceparam.platetypesnapby.Equals(thisview.deviceparam.platetypesnapby | Enum_PlateTypeSnapBy.抓拍无牌))) cjq = true;

                                if ((record.P22.Contains("黄")) && (thisview.deviceparam.platetypesnapby.Equals(thisview.deviceparam.platetypesnapby | Enum_PlateTypeSnapBy.抓拍黄牌))) cjq = true;

                                if ((record.P22.Contains("蓝")) && (thisview.deviceparam.platetypesnapby.Equals(thisview.deviceparam.platetypesnapby | Enum_PlateTypeSnapBy.抓拍蓝牌)))
                                {
                                    if ((record.P44 == "2") || (record.P44 == "5"))
                                    {
                                        cjq = true;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                #region 3获取该类型对应的抓拍参数
                SnapParam snapparam = (SnapParam)G.SystemParam.SnapParams[recordtype];
                if (snapparam.snapcount != int.Parse(record.P3))
                {
                    LogTool.log.Info(thisview.roadparam.roadname + "设备" + thisview.deviceparam.ip + recordtype.ToString() + "抓拍张数配置错误，配置=" + snapparam.snapcount.ToString() + "实际=" + record.P3);
                    return;
                }
                #endregion

                #region 4图片合成

                bool joinflag = false;
                if ((thisview.deviceparam.CanJoin == false) &&
                    (((G.SystemParam.TwoPicsJoinMode != Enum_PicJoinMode.No) && (snapparam.snapcount == 2)) ||
                    ((G.SystemParam.ThreePicsJoinMode != Enum_PicJoinMode.No) && (snapparam.snapcount == 3)) ||
                    ((G.SystemParam.FourPicsJoinMode != Enum_PicJoinMode.No) && (snapparam.snapcount == 4))))
                {
                    double left = 0;
                    double top = 0;
                    double right = 0;
                    double down = 0;
                    switch (record.P17)
                    {
                        case "1":
                            left = thisview.focusparam.left1;
                            top = thisview.focusparam.top1;
                            right = thisview.focusparam.right1;
                            down = thisview.focusparam.down1;
                            break;
                        case "2":
                            left = thisview.focusparam.left2;
                            top = thisview.focusparam.top2;
                            right = thisview.focusparam.right2;
                            down = thisview.focusparam.down2;
                            break;
                        case "3":
                            left = thisview.focusparam.left3;
                            top = thisview.focusparam.top3;
                            right = thisview.focusparam.right3;
                            down = thisview.focusparam.down3;
                            break;
                        case "4":
                            left = thisview.focusparam.left4;
                            top = thisview.focusparam.top4;
                            right = thisview.focusparam.right4;
                            down = thisview.focusparam.down4;
                            break;
                        case "5":
                            left = thisview.focusparam.left5;
                            top = thisview.focusparam.top5;
                            right = thisview.focusparam.right5;
                            down = thisview.focusparam.down5;
                            break;
                        case "6":
                            left = thisview.focusparam.left6;
                            top = thisview.focusparam.top6;
                            right = thisview.focusparam.right6;
                            down = thisview.focusparam.down6;
                            break;
                        default:
                            break;
                    }

                    switch (snapparam.snapcount)//要求抓拍的张数
                    {
                        case 1:
                            break;
                        case 2:
                            if ((G.SystemParam.TwoPicsJoinMode == Enum_PicJoinMode.A2) || (G.SystemParam.TwoPicsJoinMode == Enum_PicJoinMode.B2))
                            {
                                joinflag = JoinPic(G.SystemParam.TwoPicsJoinMode, record.Pic1, record.Pic2, null, null, snapparam.platefrom, left, top, right, down, ref joinedpic);
                                if (LogTool.gWorkEnable) LogTool.log.Info(thisview.roadparam.roadname + "设备" + thisview.deviceparam.ip + recordtype.ToString() + "2张图片合成" + (joinflag ? "成功" : "失败") + record.P19);
                            }
                            break;
                        case 3:
                            if ((G.SystemParam.ThreePicsJoinMode == Enum_PicJoinMode.A3) || (G.SystemParam.ThreePicsJoinMode == Enum_PicJoinMode.B3))
                            {
                                joinflag = JoinPic(G.SystemParam.ThreePicsJoinMode, record.Pic1, record.Pic2, record.Pic3, null, snapparam.platefrom, left, top, right, down, ref joinedpic);
                                if (LogTool.gWorkEnable) LogTool.log.Info(thisview.roadparam.roadname + "设备" + thisview.deviceparam.ip + recordtype.ToString() + "3张图片合成" + (joinflag ? "成功" : "失败") + record.P19);
                            }
                            break;
                        case 4:
                            if ((G.SystemParam.FourPicsJoinMode == Enum_PicJoinMode.A4) || (G.SystemParam.FourPicsJoinMode == Enum_PicJoinMode.B4))
                            {
                                joinflag = JoinPic(G.SystemParam.FourPicsJoinMode, record.Pic1, record.Pic2, record.Pic3, record.Pic4, snapparam.platefrom, left, top, right, down, ref joinedpic);
                                if (LogTool.gWorkEnable) LogTool.log.Info(thisview.roadparam.roadname + "设备" + thisview.deviceparam.ip + recordtype.ToString() + "4张图片合成" + (joinflag ? "成功" : "失败") + record.P19);
                            }
                            break;
                        default:
                            break;
                    }
                    //保存合成的图片数据，冲掉单张场景图片数据
                    if ((joinflag == true) && (joinedpic != null))
                    {
                        record.Pic1 = joinedpic;
                        record.Pic2 = null;
                        record.Pic3 = null;
                        record.Pic4 = null;
                    }
                }
                #endregion

                #region 5叠加字符
                string fx = "";
                switch (record.P16)
                {
                    case "1":
                        fx = "由东向西";
                        break;
                    case "2":
                        fx = "由南向北";
                        break;
                    case "3":
                        fx = "由西向东";
                        break;
                    case "4":
                        fx = "由北向南";
                        break;
                    default:
                        break;
                }
                if ((G.SystemParam.PrintEnable == true) && (thisview.deviceparam.CanPrint == false))
                {
                    string printstring = "";
                    if (G.SystemParam.PrintTime) printstring += record.P19 + " ";
                    if (G.SystemParam.PrintRoad) printstring += record.P15 + " ";
                    if (G.SystemParam.PrintDirection) printstring += fx + " ";
                    if (G.SystemParam.PrintCD) printstring += ("车道" + record.P17 + " ");
                    if (G.SystemParam.PrintCaptureSystem) printstring += recordtype.ToString() + " ";
                    if (G.SystemParam.PrintPlateColor) printstring += record.P22 + " ";
                    if (G.SystemParam.PrintHPHM) printstring += record.P23 + " ";
                    if (G.SystemParam.PrintSBBH) printstring += ("设备号" + record.P8 + " ");
                    if (G.SystemParam.PrintFWM) printstring += ("防伪码" + Guid.NewGuid().ToString("N"));

                    byte[] tempbytes = null;
                    if (record.Pic1 != null)
                    {
                        tempbytes = Print(record.Pic1, G.SystemParam.PrintPosition, printstring);
                        if (tempbytes != null) record.Pic1 = tempbytes;
                    }
                    if (record.Pic2 != null)
                    {
                        tempbytes = Print(record.Pic2, G.SystemParam.PrintPosition, printstring);
                        if (tempbytes != null) record.Pic2 = tempbytes;
                    }
                    if (record.Pic3 != null)
                    {
                        tempbytes = Print(record.Pic3, G.SystemParam.PrintPosition, printstring);
                        if (tempbytes != null) record.Pic3 = tempbytes;
                    }
                    if (record.Pic4 != null)
                    {
                        tempbytes = Print(record.Pic4, G.SystemParam.PrintPosition, printstring);
                        if (tempbytes != null) record.Pic4 = tempbytes;
                    }
                }
                #endregion

                #region 6图片压缩

                if (
                    (G.SystemParam.DJCompressEnable == true)
                    //&& (thisview.deviceparam.CanCompress == false) 
                    && (recordtype != Enum_CaptureSystem.卡口)
                    )
                {
                    int limit = int.Parse(G.SystemParam.DJCompressValue) * 1024;
                    byte[] tempbytes = null;

                    if ((record.Pic1 != null) && (record.Pic1.Length > limit))
                    {
                        picCompress.picBytesPressLimit(record.Pic1, out tempbytes, limit);
                        if (tempbytes != null) record.Pic1 = tempbytes;
                    }
                    if ((record.Pic2 != null))
                    {
                        picCompress.picBytesPressLimit(record.Pic2, out tempbytes, limit);
                        if (tempbytes != null) record.Pic2 = tempbytes;
                    }
                    if ((record.Pic3 != null))
                    {
                        picCompress.picBytesPressLimit(record.Pic3, out tempbytes, limit);
                        if (tempbytes != null) record.Pic3 = tempbytes;
                    }
                    if ((record.Pic4 != null))
                    {
                        picCompress.picBytesPressLimit(record.Pic4, out tempbytes, limit);
                        if (tempbytes != null) record.Pic4 = tempbytes;
                    }
                }
                #endregion

                #region 7图片尾部信息
                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    if (G.SystemParam.KKTailEnable)
                    {
                        JariTail tail = new JariTail();
                        tail.m_Info1 = "";
                        tail.m_Info2 = "";
                        tail.m_Info3 = "0";
                        tail.m_Info4 = record.P23;
                        string info5 = "";
                        switch (record.P22)
                        {
                            case "蓝":
                                info5 = "0";
                                break;
                            case "黄":
                                info5 = "1";
                                break;
                            case "白":
                                info5 = "2";
                                break;
                            case "黑":
                                info5 = "3";
                                break;
                            default:
                                info5 = "";
                                break;
                        }
                        tail.m_Info5 = info5;
                        tail.m_Info6 = "";
                        tail.m_Info7 = record.P24;
                        tail.m_Info8 = "";
                        tail.m_Info9 = "";
                        tail.m_Info10 = record.P5;
                        tail.m_Info11 = record.P25;
                        tail.m_Info12 = record.P15;
                        tail.m_Info13 = record.P17;
                        tail.m_Info14 = "";
                        tail.m_Info15 = record.P17;
                        tail.m_Info16 = record.P18.Substring(14, 3);
                        tail.m_Info17 = record.P14;
                        tail.m_Info18 = record.P19;
                        tail.m_Info19 = record.P16;
                        tail.m_Info20 = "车道" + record.P17;
                        tail.m_Info21 = record.P26;
                        tail.m_Info22 = record.P27;
                        tail.m_Info23 = "";
                        tail.m_Info24 = "";
                        if (record.Pic1 != null) record.Pic1 = tail.AddTailToPic(record.Pic1);
                        if (record.Pic2 != null) record.Pic2 = tail.AddTailToPic(record.Pic2);
                        if (record.Pic3 != null) record.Pic3 = tail.AddTailToPic(record.Pic3);
                        if (record.Pic4 != null) record.Pic4 = tail.AddTailToPic(record.Pic4);
                        if (record.Pic5 != null) record.Pic5 = tail.AddTailToPic(record.Pic5);
                        if (record.Pic6 != null) record.Pic6 = tail.AddTailToPic(record.Pic6);
                        if (record.Pic7 != null) record.Pic7 = tail.AddTailToPic(record.Pic7);
                    }
                }
                else
                {
                    if (G.SystemParam.DJTailEnable)
                    {
                        JariTail tail = new JariTail();
                        tail.m_Info1 = "";
                        tail.m_Info2 = "";
                        tail.m_Info3 = "0";
                        tail.m_Info4 = record.P23;
                        string info5 = "";
                        switch (record.P22)
                        {
                            case "蓝":
                                info5 = "0";
                                break;
                            case "黄":
                                info5 = "1";
                                break;
                            case "白":
                                info5 = "2";
                                break;
                            case "黑":
                                info5 = "3";
                                break;
                            default:
                                info5 = "";
                                break;
                        }
                        tail.m_Info5 = info5;
                        tail.m_Info6 = "";
                        tail.m_Info7 = record.P24;
                        tail.m_Info8 = "";
                        tail.m_Info9 = "";
                        tail.m_Info10 = "3";//睢宁都是3=record.P5;
                        tail.m_Info11 = record.P25;
                        tail.m_Info12 = record.P15;
                        tail.m_Info13 = record.P17;
                        tail.m_Info14 = "";
                        tail.m_Info15 = record.P17;
                        tail.m_Info16 = record.P18.Substring(14, 3);
                        tail.m_Info17 = record.P14;
                        tail.m_Info18 = record.P19;
                        tail.m_Info19 = record.P16;
                        tail.m_Info20 = "车道" + record.P17;
                        tail.m_Info21 = record.P26;
                        tail.m_Info22 = record.P27;
                        tail.m_Info23 = "";
                        tail.m_Info24 = "";
                        if (record.Pic1 != null) record.Pic1 = tail.AddTailToPic(record.Pic1);
                        if (record.Pic2 != null) record.Pic2 = tail.AddTailToPic(record.Pic2);
                        if (record.Pic3 != null) record.Pic3 = tail.AddTailToPic(record.Pic3);
                        if (record.Pic4 != null) record.Pic4 = tail.AddTailToPic(record.Pic4);
                        if (record.Pic5 != null) record.Pic5 = tail.AddTailToPic(record.Pic5);
                        if (record.Pic6 != null) record.Pic6 = tail.AddTailToPic(record.Pic6);
                        if (record.Pic7 != null) record.Pic7 = tail.AddTailToPic(record.Pic7);
                    }
                }
                #endregion

                #region 8图片存储

                string yyyy = record.P18.Substring(0, 4);
                string MM = record.P18.Substring(4, 2);
                string dd = record.P18.Substring(6, 2);
                string HH = record.P18.Substring(8, 2);
                string mm = record.P18.Substring(10, 2);
                string ss = record.P18.Substring(12, 2);
                string fff = record.P18.Substring(14, 3);
                string path = "";
                string head = "";
                string name1 = "";
                string name2 = "";
                string name3 = "";
                string name4 = "";
                string name5 = "";
                string name6 = "";
                string name7 = "";
                string webpath = "";

                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    if (cjq == false)
                    {
                        path = thisview.roadparam.kkpath;
                        if (G.SystemParam.WebPathType == Enum_WebPathType.FTP)
                        {
                            webpath = "ftp://" + G.SystemParam.LocalIP + "/" + thisview.roadparam.kkvpath;
                        }
                        else
                        {
                            webpath = "http://" + G.SystemParam.LocalIP + "/" + thisview.roadparam.kkvpath;
                        }
                        //卡口文件夹的模式
                        switch (G.SystemParam.KKSubFolderMode)
                        {
                            case Enum_SubFolderMode.地点代码_年_月_日:
                                path += ("\\" + thisview.roadparam.gbdm + "\\" + yyyy + "\\" + MM + "\\" + dd);
                                webpath += ("/" + thisview.roadparam.gbdm + "/" + yyyy + "/" + MM + "/" + dd);
                                break;
                            case Enum_SubFolderMode.路口名称_年_月_日:
                                path += ("\\" + thisview.roadparam.roadname + "\\" + yyyy + "\\" + MM + "\\" + dd);
                                webpath += ("/" + thisview.roadparam.roadname + "/" + yyyy + "/" + MM + "/" + dd);
                                break;
                            case Enum_SubFolderMode.年月日_路口名称:
                                path += ("\\" + yyyy + MM + dd + "\\" + thisview.roadparam.roadname);
                                webpath += ("/" + yyyy + MM + dd + "/" + thisview.roadparam.roadname);
                                break;
                            case Enum_SubFolderMode.友上:
                                path += ("\\data" + "\\" + yyyy + MM + dd + "\\" + thisview.roadparam.roadid + "\\pic");
                                webpath += path.Replace("\\", "/");
                                break;
                            case Enum_SubFolderMode.自定义:
                                string ssub = GetDIYSubPath(G.SystemParam.KKDiy, thisview.roadparam.roadid, record.P17, record.P16,
                                    fx, thisview.roadparam.gbdm, thisview.roadparam.roadname, yyyy, MM, dd,
                                    thisview.deviceparam.sbbh, recordtype.ToString(), HH, record.P6);
                                path += ssub;
                                webpath += ssub.Replace("\\", "/");
                                break;
                            default:
                                break;
                        }
                        //卡口图片模式
                        switch (G.SystemParam.KKPicNameMode)
                        {
                            case Enum_PicNameMode.标准模式:
                                head = thisview.roadparam.roadid + record.P16 + record.P17 + "-" + yyyy + MM + dd + HH + mm + ss + "-" + fff + "-" + snapparam.snapcount.ToString() + "-";
                                name1 = head + "1.jpg";
                                name2 = head + "2.jpg";
                                name3 = head + "3.jpg";
                                name4 = head + "4.jpg";
                                name5 = head + "5.jpg";
                                name6 = head + "6.jpg";
                                name7 = head + "7.jpg";
                                break;
                            case Enum_PicNameMode.友上协议:
                                YoSunStruct YSStruct = new YoSunStruct();
                                YSStruct = YoSunAdapter.trans(record);
                                head = YSStruct.fileName.Replace(".jpg", string.Empty);
                                name1 = head + "1.jpg";
                                name2 = head + "2.jpg";
                                name3 = head + "3.jpg";
                                name4 = head + "4.jpg";
                                name5 = head + "5.jpg";
                                name6 = head + "6.jpg";
                                name7 = head + "7.jpg";
                                break;
                            case Enum_PicNameMode.自定义:
                                head = GetDIYSubName(G.SystemParam.KKNameSplitor, G.SystemParam.KKNameDiy, record.P, yyyy, MM, dd, HH, mm, ss, fff);
                                name1 = head + "1.jpg";
                                //武汉自定义修改
                                name1 = name1.Replace(G.SystemParam.DJNameSplitor + "1.jpg", ".jpg");
                                name2 = head + "2.jpg";
                                name3 = head + "3.jpg";
                                name4 = head + "4.jpg";
                                name5 = head + "5.jpg";
                                name6 = head + "6.jpg";
                                name7 = head + "7.jpg";
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        recordtype = Enum_CaptureSystem.黄牌禁限;
                        record.P3 = "2";//卡口筛选的闯禁区为2张
                        record.P5 = "11";
                        record.P7 = "1";
                        record.P10 = ((VioParam)G.SystemParam.Wfxwdms[Enum_VioType.黄牌禁限]).viocode;//加载违法代码
                        //静态标牌图片

                        if (thisview.deviceparam.staticpicenable == true)
                        {
                            if (File.Exists(thisview.deviceparam.staticpic))
                            {
                                switch (thisview.deviceparam.staticpicindex)
                                {
                                    case 1:
                                        record.Pic1 = File.ReadAllBytes(thisview.deviceparam.staticpic);
                                        break;
                                    case 2:
                                        record.Pic2 = File.ReadAllBytes(thisview.deviceparam.staticpic);
                                        break;
                                    case 3:
                                        record.Pic3 = File.ReadAllBytes(thisview.deviceparam.staticpic);
                                        break;
                                    case 4:
                                        record.Pic4 = File.ReadAllBytes(thisview.deviceparam.staticpic);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }
                }

                if (recordtype != Enum_CaptureSystem.卡口)
                {
                    path = thisview.roadparam.djpath;
                    if (G.SystemParam.WebPathType == Enum_WebPathType.FTP)
                    {
                        webpath = "ftp://" + G.SystemParam.LocalIP + "/" + thisview.roadparam.djvpath;
                    }
                    else
                    {
                        webpath = "http://" + G.SystemParam.LocalIP + "/" + thisview.roadparam.djvpath;
                    }

                    switch (G.SystemParam.DJSubFolderMode)
                    {
                        case Enum_SubFolderMode.地点代码_年_月_日:
                            path += ("\\" + thisview.roadparam.gbdm + "\\" + yyyy + "\\" + MM + "\\" + dd);
                            webpath += ("/" + thisview.roadparam.gbdm + "/" + yyyy + "/" + MM + "/" + dd);
                            break;
                        case Enum_SubFolderMode.路口名称_年_月_日:
                            path += ("\\" + thisview.roadparam.roadname + "\\" + yyyy + "\\" + MM + "\\" + dd);
                            webpath += ("/" + thisview.roadparam.roadname + "/" + yyyy + "/" + MM + "/" + dd);
                            break;
                        case Enum_SubFolderMode.年月日_路口名称:
                            path += ("\\" + yyyy + MM + dd + "\\" + thisview.roadparam.roadname);
                            webpath += ("/" + yyyy + MM + dd + "/" + thisview.roadparam.roadname);
                            break;
                        case Enum_SubFolderMode.友上:
                            path += ("\\" + yyyy + MM + dd + "\\" + thisview.roadparam.roadname + "\\data");
                            webpath += ("/" + yyyy + MM + dd + "/" + thisview.roadparam.roadname);
                            break;
                        case Enum_SubFolderMode.自定义:
                            string ssub = GetDIYSubPath(G.SystemParam.DJDiy, thisview.roadparam.roadid, record.P17, record.P16,
                                fx, thisview.roadparam.gbdm, thisview.roadparam.roadname, yyyy, MM, dd,
                                thisview.deviceparam.sbbh, recordtype.ToString(), HH, record.P6);
                            path += ssub;
                            webpath += ssub.Replace("\\", "/");
                            break;
                        default:
                            break;
                    }
                    switch (G.SystemParam.DJPicNameMode)
                    {
                        case Enum_PicNameMode.标准模式:
                            head = thisview.roadparam.roadid + record.P16 + record.P17 + "-" + yyyy + MM + dd + HH + mm + ss + "-" + fff + "-" + snapparam.snapcount.ToString() + "-";
                            name1 = head + "1.jpg";
                            name2 = head + "2.jpg";
                            name3 = head + "3.jpg";
                            name4 = head + "4.jpg";
                            name5 = head + "5.jpg";
                            name6 = head + "6.jpg";
                            name7 = head + "7.jpg";
                            break;
                        case Enum_PicNameMode.友上协议:
                            YoSunStruct YSStruct = new YoSunStruct();
                            YSStruct = YoSunAdapter.trans(record);
                            head = YSStruct.fileName;
                            name1 = head + "1.jpg";
                            name2 = head + "2.jpg";
                            name3 = head + "3.jpg";
                            name4 = head + "4.jpg";
                            name5 = head + "5.jpg";
                            name6 = head + "6.jpg";
                            name7 = head + "7.jpg";
                            break;
                        case Enum_PicNameMode.自定义:
                            head = GetDIYSubName(G.SystemParam.DJNameSplitor, G.SystemParam.DJNameDiy, record.P, yyyy, MM, dd, HH, mm, ss, fff);
                            name1 = head + "1.jpg";
                            //武汉自定义修改
                            name1 = name1.Replace(G.SystemParam.DJNameSplitor + "1.jpg", ".jpg");
                            name2 = head + "2.jpg";
                            name3 = head + "3.jpg";
                            name4 = head + "4.jpg";
                            name5 = head + "5.jpg";
                            name6 = head + "6.jpg";
                            name7 = head + "7.jpg";
                            break;
                        case Enum_PicNameMode.麻城模式:
                            //（12位设备编码）_1_420901000000_600152006000_20100412174340222_7207_000_000_1_1_鄂K00000_02-1.jpg
                            //（12位设备编码）_采集方式代码+“_”+采集单位代码+“_”+违法地点代码+“_”+违法时间+“_”+违法行为代码+“_”+实测速度+“_”+限制速度+“_”+车道代码+“_”+行驶方向代码+“_”+号牌号码+“_”+号牌种类+"-"+图片序号.jpg
                            head = thisview.deviceparam.sbbh + "_" + record.P7 + "_" + thisview.roadparam.bmbh + "_" + thisview.roadparam.gbdm + "_" + record.P18 + "_" + record.P10 + "_" + record.P27 + "_" + record.P26 + "_" + record.P17 + "_" + record.P16 + "_" + record.P23 + "_" + record.P21 + "-";
                            name1 = head + "1.jpg";
                            name2 = head + "2.jpg";
                            name3 = head + "3.jpg";
                            name4 = head + "4.jpg";
                            name5 = head + "5.jpg";
                            name6 = head + "6.jpg";
                            name7 = head + "7.jpg";
                            break;
                        default:
                            break;
                    }
                }

                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                if (record.Pic1 != null)
                {

                    record.P30 = path + "\\" + name1;
                    record.P37 = webpath + "/" + name1;
                    File.WriteAllBytes(record.P30, record.Pic1);
                    if (ValidateJpg(record.Pic1) == false) return;
                }
                if (record.Pic2 != null)
                {
                    record.P31 = path + "\\" + name2;
                    record.P38 = webpath + "/" + name2;
                    File.WriteAllBytes(record.P31, record.Pic2);
                    if (ValidateJpg(record.Pic2) == false) return;
                }
                if (record.Pic3 != null)
                {
                    record.P32 = path + "\\" + name3;
                    record.P39 = webpath + "/" + name3;
                    File.WriteAllBytes(record.P32, record.Pic3);
                    if (ValidateJpg(record.Pic3) == false) return;
                }
                if (record.Pic4 != null)
                {
                    record.P33 = path + "\\" + name4;
                    record.P40 = webpath + "/" + name4;
                    File.WriteAllBytes(record.P33, record.Pic4);
                    if (ValidateJpg(record.Pic4) == false) return;
                }
                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    if (G.SystemParam.KKPlateEnable)
                    {
                        if (record.Pic5 != null)
                        {
                            record.P34 = path + "\\" + name5;
                            record.P41 = webpath + "/" + name5;
                            File.WriteAllBytes(record.P34, record.Pic5);
                            if (ValidateJpg(record.Pic5) == false) return;
                        }
                    }
                }
                else
                {
                    if (G.SystemParam.DJPlateEnable)
                    {
                        if (record.Pic5 != null)
                        {
                            record.P34 = path + "\\" + name5;
                            record.P41 = webpath + "/" + name5;
                            File.WriteAllBytes(record.P34, record.Pic5);
                            if (ValidateJpg(record.Pic5) == false) return;
                        }
                    }
                }
                if (record.Pic6 != null)
                {
                    record.P35 = path + "\\" + name6;
                    record.P42 = webpath + "/" + name6;
                    File.WriteAllBytes(record.P35, record.Pic6);
                    if (ValidateJpg(record.Pic6) == false) return;
                }
                if (record.Pic7 != null)
                {
                    record.P36 = path + "\\" + name7;
                    record.P43 = webpath + "/" + name7;
                    File.WriteAllBytes(record.P36, record.Pic7);
                    if (ValidateJpg(record.Pic7) == false) return;
                }



                #endregion

                #region 9数据上传
                bool send = false;
                string error = "";
                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    #region 入库
                    if (G.SystemParam.KKUploadEnable)
                    {
                        Msg msg = new Msg();
                        msg.type = Enum_ProtocolType.Upload;
                        msg.uuid = Guid.NewGuid().ToString("N");
                        msg.feedback = true;
                        msg.data = record.ToBytes();
                        msg.length = msg.data.Length;

                        send = G.KKClient.Send(msg, ref error);
                        if (LogTool.gWorkEnable) LogTool.log.Info((send ? "卡口上传成功" : "卡口上传失败") + record.P19 + record.P15 + error);

                        if (send == true)
                        {
                            //更新经过时间
                            bag.state.LastJGSJ = DateTime.Parse(record.P19);

                            //更新无数据标记
                            if (bag.state.NoData == true)
                            {
                                bag.state.NoData = false;
                                bag.state.NoDataTime = DateTime.Parse(record.P19);
                            }

                            if (G.KKCount != null)
                            {
                                for (int i = 0; i < G.KKCount.Count; i++)
                                {
                                    if (G.KKCount[i].ip == thisview.deviceparam.ip)
                                    {
                                        int hour = 0;
                                        try
                                        {
                                            hour = int.Parse(HH);
                                        }
                                        catch { }
                                        if ((hour >= 0) && (hour <= 23))
                                        {
                                            G.KKCount[i].h[hour + 1]++;
                                            G.KKCount[i].h[0]++;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //更新上传故障
                            bag.state.UploadFail = true;
                            bag.state.UploadTime = DateTime.Parse(record.P19);

                            if ((G.SystemParam.KKFailReTryEnable) && (record.P5 == "0"))
                            {
                                G.SaveFailRecord(record);
                            }
                        }

                    }
                    #endregion
                }
                else
                {
                    #region 电警入库
                    if (G.SystemParam.DJUploadEnable)
                    {
                        Msg msg = new Msg();
                        msg.type = Enum_ProtocolType.Upload;
                        msg.uuid = Guid.NewGuid().ToString("N");
                        msg.feedback = true;
                        msg.data = record.ToBytes();
                        msg.length = msg.data.Length;

                        if (msg.data.Length != 0)
                        {
                            send = G.DJClient.Send(msg, ref error);
                            if (LogTool.gWorkEnable) LogTool.log.Info((send ? "电警上传成功" : "电警上传失败") + record.P19 + record.P15 + error);

                        }

                        if (send == true)
                        {
                            if (G.DJCount != null)
                            {
                                for (int i = 0; i < G.DJCount.Count; i++)
                                {
                                    if (G.DJCount[i].ip == thisview.deviceparam.ip)
                                    {
                                        int hour = 0;
                                        try
                                        {
                                            hour = int.Parse(HH);
                                        }
                                        catch { }
                                        if ((hour >= 0) && (hour <= 23))
                                        {
                                            G.DJCount[i].h[hour + 1]++;
                                            G.DJCount[i].h[0]++;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if ((G.SystemParam.DJFailReTryEnable) && (record.P5 != "0"))
                            {
                                G.SaveFailRecord(record);
                            }
                        }
                    }
                    #endregion
                }
                #endregion

                #region 10数据推送
                if (recordtype == Enum_CaptureSystem.卡口)
                {
                    #region 推送
                    try
                    {
                        if (G.SystemParam.KKPushEnable)
                        {
                            PicCenterPush push = PicCenterPush.Parse(record.P);

                            Msg pushmsg = new Msg();
                            pushmsg.type = Enum_ProtocolType.PicCenterPush;
                            pushmsg.uuid = Guid.NewGuid().ToString("N");
                            pushmsg.feedback = (G.SystemParam.PushWorkMode == Enum_TcpWorkMode.SendRead) ? true : false;
                            pushmsg.data = push.ToBytes();
                            pushmsg.length = pushmsg.data.Length;

                            bool flag = G.PushServer.Push(pushmsg, ref error);
                            if (LogTool.gWorkEnable) LogTool.log.Info((flag ? "卡口推送成功" : "卡口推送失败") + push.P19 + push.P15 + error);
                            if ((flag == false) && (G.SystemParam.KKRePushEnable) && (push.P5 == "0"))
                            {
                                G.SavePushRecord(push);
                            }
                        }
                    }
                    catch (Exception er)
                    {
                        LogTool.log.Info("卡口推送异常" + record.P19 + record.P15 + er.Message);
                    }
                    #endregion
                }
                else
                {
                    #region 电警推送
                    try
                    {
                        if (G.SystemParam.DJPushEnable)
                        {
                            PicCenterPush push = PicCenterPush.Parse(record.P);

                            Msg pushmsg = new Msg();
                            pushmsg.type = Enum_ProtocolType.PicCenterPush;
                            pushmsg.uuid = Guid.NewGuid().ToString("N");
                            pushmsg.feedback = (G.SystemParam.PushWorkMode == Enum_TcpWorkMode.SendRead) ? true : false;
                            pushmsg.data = push.ToBytes();
                            pushmsg.length = pushmsg.data.Length;

                            bool flag = G.PushServer.Push(pushmsg, ref error);
                            if (LogTool.gWorkEnable) LogTool.log.Info((flag ? "电警推送成功" : "电警推送失败") + push.P19 + push.P15 + error);
                            if ((flag == false) && (G.SystemParam.DJRePushEnable) && (push.P5 != "0"))
                            {
                                G.SavePushRecord(push);
                            }
                        }
                    }
                    catch (Exception er)
                    {
                        LogTool.log.Info("电警推送异常" + record.P19 + record.P15 + er.Message);
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception er)
            {
                LogTool.log.Info("Worker.DoWork()" + bag.Txt() + er.Message);
            }
            finally
            {
                GC.Collect();
            }
        }

        private static bool ValidateJpg(byte[] jpgbytes)
        {
            bool ans = false;
            MemoryStream ms = null;
            Image img = null;
            try
            {
                ms = new MemoryStream(jpgbytes);
                img = Image.FromStream(ms);
                if ((img.Width > 0) && (img.Height > 0))
                {
                    ans = true;
                }
            }
            catch { ans = false; }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
                if (img != null)
                {
                    img.Dispose();
                }
            }
            return ans;
        }

        private static string GetDIYSubName(string splitor, string namediy, string[] P, string yyyy, string MM, string dd, string HH, string mm, string ss, string fff)
        {
            string ans = "";
            try
            {
                if ((splitor != "") && (namediy != ""))
                {
                    string[] subnames = namediy.Split(new string[] { splitor }, StringSplitOptions.RemoveEmptyEntries);
                    if (subnames != null)
                    {
                        foreach (string subnamestring in subnames)
                        {
                            string nameby = subnamestring;
                            string spchar = "";
                            if (subnamestring.StartsWith("指定字符"))
                            {
                                nameby = subnamestring.Replace("指定字符", "");
                                spchar = subnamestring.Remove(0, 4);
                            }
                            Enum_DiyName subname = (Enum_DiyName)Enum.Parse(typeof(Enum_DiyName), nameby);
                            switch (subname)
                            {
                                case Enum_DiyName.协议版本号:
                                    ans += (P[0] + splitor);
                                    break;
                                case Enum_DiyName.图片组序号:
                                    ans += (P[1] + splitor);
                                    break;
                                case Enum_DiyName.一组图片数量:
                                    ans += (P[2] + splitor);
                                    break;
                                case Enum_DiyName.图片合成模式:
                                    ans += (P[3] + splitor);
                                    break;
                                case Enum_DiyName.记录类型:
                                    ans += (P[4] + splitor);
                                    break;
                                case Enum_DiyName.数据提供厂家:
                                    ans += (P[5] + splitor);
                                    break;
                                case Enum_DiyName.采集方式:
                                    ans += (P[6] + splitor);
                                    break;
                                case Enum_DiyName.设备编号:
                                    ans += (P[7] + splitor);
                                    break;
                                case Enum_DiyName.采集人员:
                                    ans += (P[8] + splitor);
                                    break;
                                case Enum_DiyName.违法行为代码:
                                    ans += (P[9] + splitor);
                                    break;
                                case Enum_DiyName.路口国标编号:
                                    ans += (P[10] + splitor);
                                    break;
                                case Enum_DiyName.部门编号:
                                    ans += (P[11] + splitor);
                                    break;
                                case Enum_DiyName.行政区划:
                                    ans += (P[12] + splitor);
                                    break;
                                case Enum_DiyName.路口厂家编号:
                                    ans += (P[13] + splitor);
                                    break;
                                case Enum_DiyName.路口名称:
                                    ans += (P[14] + splitor);
                                    break;
                                case Enum_DiyName.方向:
                                    ans += (P[15] + splitor);
                                    break;
                                case Enum_DiyName.车道:
                                    ans += (P[16] + splitor);
                                    break;
                                case Enum_DiyName.号牌种类:
                                    ans += (P[20] + splitor);
                                    break;
                                case Enum_DiyName.车牌颜色:
                                    ans += (P[21] + splitor);
                                    break;
                                case Enum_DiyName.车牌号:
                                    ans += (P[22] + splitor);
                                    break;
                                case Enum_DiyName.车身颜色:
                                    ans += (P[23] + splitor);
                                    break;
                                case Enum_DiyName.车辆长度:
                                    ans += (P[24] + splitor);
                                    break;
                                case Enum_DiyName.限速:
                                    ans += (P[25] + splitor);
                                    break;
                                case Enum_DiyName.车速:
                                    ans += (P[26] + splitor);
                                    break;
                                case Enum_DiyName.数据标识:
                                    ans += (P[27] + splitor);
                                    break;
                                case Enum_DiyName.图片服务器IP:
                                    ans += (P[28] + splitor);
                                    break;
                                case Enum_DiyName.年月日时分秒毫秒:
                                    ans += (yyyy + MM + dd + HH + mm + ss + fff + splitor);
                                    break;
                                case Enum_DiyName.年:
                                    ans += (yyyy + splitor);
                                    break;
                                case Enum_DiyName.月:
                                    ans += (MM + splitor);
                                    break;
                                case Enum_DiyName.日:
                                    ans += (dd + splitor);
                                    break;
                                case Enum_DiyName.时:
                                    ans += (HH + splitor);
                                    break;
                                case Enum_DiyName.分:
                                    ans += (mm + splitor);
                                    break;
                                case Enum_DiyName.秒:
                                    ans += (ss + splitor);
                                    break;
                                case Enum_DiyName.毫秒:
                                    ans += (fff + splitor);
                                    break;
                                case Enum_DiyName.年月日:
                                    ans += (yyyy + MM + dd + splitor);
                                    break;
                                case Enum_DiyName.时分秒:
                                    ans += (HH + mm + ss + splitor);
                                    break;
                                case Enum_DiyName.时分秒毫秒:
                                    ans += (HH + mm + ss + fff + splitor);
                                    break;
                                case Enum_DiyName.车辆类型:
                                    ans += (P[43] + splitor);
                                    break;
                                case Enum_DiyName.车辆品牌:
                                    ans += (P[44] + splitor);
                                    break;
                                case Enum_DiyName.车牌坐标:
                                    ans += (P[45] + splitor);
                                    break;
                                case Enum_DiyName.指定字符:
                                    ans += spchar;
                                    break;
                            }
                        }
                    }
                }
            }
            catch { ans = ""; }
            return ans;
        }

        private static string GetDIYSubPath(string diymode,string roadid,string cd,string dirid,string fx,string gbdm,string roadname,string yyyy,string MM,string dd,string sbbh,string zpxt,string HH,string cj)
        {
            string ans = "";
            try
            {
                if (diymode != "")
                {
                    string[] ss = diymode.Split('\\');
                    if (ss != null)
                    {
                        foreach (string s in ss)
                        {
                            Enum_DiySubFolder diy = (Enum_DiySubFolder)Enum.Parse(typeof(Enum_DiySubFolder), s);
                            switch (diy)
                            { 
                                case Enum_DiySubFolder.厂家编号:
                                    ans += ("\\" + roadid);
                                    break;
                                case Enum_DiySubFolder.车道代号:
                                    ans += ("\\" + cd);
                                    break;
                                case Enum_DiySubFolder.方向代号:
                                    ans += ("\\" + dirid);
                                    break;
                                case Enum_DiySubFolder.方向名称:
                                    ans += ("\\" + fx);
                                    break;
                                case Enum_DiySubFolder.国标代码:
                                    ans += ("\\" + gbdm);
                                    break;
                                case Enum_DiySubFolder.路口名称:
                                    ans += ("\\" + roadname);
                                    break;
                                case Enum_DiySubFolder.年:
                                    ans += ("\\" + yyyy);
                                    break;
                                case Enum_DiySubFolder.年月日:
                                    ans += ("\\" + yyyy + MM + dd);
                                    break;
                                case Enum_DiySubFolder.日:
                                    ans += ("\\" + dd);
                                    break;
                                case Enum_DiySubFolder.设备编号:
                                    ans += ("\\" + sbbh);
                                    break;
                                case Enum_DiySubFolder.月:
                                    ans += ("\\" + MM);
                                    break;
                                case Enum_DiySubFolder.抓拍系统:
                                    ans += ("\\" + zpxt);
                                    break;
                                case Enum_DiySubFolder.小时:
                                    ans += ("\\" + HH);
                                    break;
                                case Enum_DiySubFolder.厂家标志:
                                    ans += ("\\" + cj);
                                    break;
                                case Enum_DiySubFolder.年月:
                                    ans += ("\\" + yyyy + MM);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
            catch { ans = ""; }
            return ans;
        }

        /// <summary>
        /// 图片合成
        /// </summary>
        /// <param name="mode">模式</param>
        /// <param name="pic1">图片1数据</param>
        /// <param name="pic2">图片2数据</param>
        /// <param name="pic3">图片3数据</param>
        /// <param name="pic4">图片4数据</param>
        /// <param name="platefrom">车牌来自图片几</param>
        /// <param name="left">特写区域左</param>
        /// <param name="top">特写区域上</param>
        /// <param name="right">特写区域右</param>
        /// <param name="down">特写区域下</param>
        /// <param name="joinedpic">合成图片</param>
        /// <returns>状态</returns>
        private static bool JoinPic(Enum_PicJoinMode mode, byte[] pic1, byte[] pic2, byte[] pic3, byte[] pic4, int platefrom, double left, double top, double right, double down, ref byte[] joinedpic)
        {
            bool ans = false;
            Image bmp1 = null;
            Image bmp2 = null;
            Image bmp3 = null;
            Image bmp4 = null;
            Image bmp5 = null;
            Image Thumbmp = null;
            Graphics g = null;
            MemoryStream ms1 = null;
            MemoryStream ms2 = null;
            MemoryStream ms3 = null;
            MemoryStream ms4 = null;
            MemoryStream ms5 = null;
            int W = 0;
            int H = 0;
            try
            {
                if (pic1 != null)
                {
                    ms1 = new MemoryStream(pic1);
                    bmp1 = Image.FromStream(ms1);
                    W = bmp1.Width;
                    H = bmp1.Height;
                }
                if (pic2 != null)
                {
                    ms2 = new MemoryStream(pic2);
                    bmp2 = Image.FromStream(ms2);
                }
                if (pic3 != null)
                {
                    ms3 = new MemoryStream(pic3);
                    bmp3 = Image.FromStream(ms3);
                }
                if (pic4 != null)
                {
                    ms4 = new MemoryStream(pic4);
                    bmp4 = Image.FromStream(ms4);
                }

                if ((W > 0) && (H > 0))
                {
                    switch (mode)
                    {
                        case Enum_PicJoinMode.A2:
                            if ((bmp1 != null) && (bmp2 != null))
                            {
                                bmp5 = new Bitmap(W, H);
                                g = Graphics.FromImage(bmp5);
                                ms5 = new MemoryStream();

                                Thumbmp = new Bitmap(bmp1, W / 2, H);
                                g.DrawImageUnscaled(Thumbmp, 0, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp2, W / 2, H);
                                g.DrawImageUnscaled(Thumbmp, W / 2, 0);
                                Thumbmp.Dispose();

                                bmp5.Save(ms5, ImageFormat.Jpeg);

                                joinedpic = ms5.ToArray();
                                ans = true;
                            }
                            break;
                        case Enum_PicJoinMode.B2:
                            if ((bmp1 != null) && (bmp2 != null))
                            {
                                bmp5 = new Bitmap(W, H);
                                g = Graphics.FromImage(bmp5);
                                ms5 = new MemoryStream();

                                Thumbmp = new Bitmap(bmp1, W, H / 2);
                                g.DrawImageUnscaled(Thumbmp, 0, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp2, W, H / 2);
                                g.DrawImageUnscaled(Thumbmp, 0, H / 2);
                                Thumbmp.Dispose();

                                bmp5.Save(ms5, ImageFormat.Jpeg);

                                joinedpic = ms5.ToArray();
                                ans = true;
                            }
                            break;
                        case Enum_PicJoinMode.A3:
                            if ((bmp1 != null) && (bmp2 != null) && (bmp3 != null))
                            {
                                bmp5 = new Bitmap(W, H / 3 * 4);
                                g = Graphics.FromImage(bmp5);
                                ms5 = new MemoryStream();

                                Thumbmp = new Bitmap(bmp1, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, 0, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp2, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, W / 3, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp3, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, W / 3 * 2, 0);
                                Thumbmp.Dispose();

                                g.DrawImageUnscaled(bmp2, 0, H / 3);

                                bmp5.Save(ms5, ImageFormat.Jpeg);

                                joinedpic = ms5.ToArray();
                                ans = true;
                            }
                            break;
                        case Enum_PicJoinMode.B3:
                            if ((bmp1 != null) && (bmp2 != null) && (bmp3 != null))
                            {
                                if ((platefrom < 4) && (platefrom > 0) && (left > 0) && (top > 0) && (right > 0) && (down > 0))
                                {
                                    bmp5 = new Bitmap(W, H);
                                    g = Graphics.FromImage(bmp5);
                                    ms5 = new MemoryStream();

                                    Thumbmp = new Bitmap(bmp1, W / 2, H / 2);
                                    g.DrawImageUnscaled(Thumbmp, 0, 0);
                                    Thumbmp.Dispose();

                                    Thumbmp = new Bitmap(bmp2, W / 2, H / 2);
                                    g.DrawImageUnscaled(Thumbmp, W / 2, 0);
                                    Thumbmp.Dispose();

                                    Thumbmp = new Bitmap(bmp3, W / 2, H / 2);
                                    g.DrawImageUnscaled(Thumbmp, 0, H / 2);
                                    Thumbmp.Dispose();



                                    int x = (int)(left * W);
                                    int y = (int)(top * H);
                                    int w = (int)((right - left) * W);
                                    int h = (int)((down - top) * H);
                                    switch (platefrom)
                                    {
                                        case 1:
                                            g.DrawImage(bmp1, new Rectangle(W / 2, H / 2, W / 2, H / 2), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
                                            break;
                                        case 2:
                                            g.DrawImage(bmp2, new Rectangle(W / 2, H / 2, W / 2, H / 2), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
                                            break;
                                        case 3:
                                            g.DrawImage(bmp3, new Rectangle(W / 2, H / 2, W / 2, H / 2), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);
                                            break;
                                        default:
                                            break;
                                    }

                                    bmp5.Save(ms5, ImageFormat.Jpeg);

                                    joinedpic = ms5.ToArray();
                                    ans = true;
                                }
                            }
                            break;
                        case Enum_PicJoinMode.A4:
                            if ((bmp1 != null) && (bmp2 != null) && (bmp3 != null) && (bmp4 != null))
                            {
                                bmp5 = new Bitmap(W, H / 3 * 4);
                                g = Graphics.FromImage(bmp5);
                                ms5 = new MemoryStream();

                                Thumbmp = new Bitmap(bmp1, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, 0, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp2, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, W / 3, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp3, W / 3, H / 3);
                                g.DrawImageUnscaled(Thumbmp, W / 3 * 2, 0);
                                Thumbmp.Dispose();

                                g.DrawImageUnscaled(bmp4, 0, H / 3);

                                bmp5.Save(ms5, ImageFormat.Jpeg);

                                joinedpic = ms5.ToArray();
                                ans = true;
                            }
                            break;
                        case Enum_PicJoinMode.B4:
                            if ((bmp1 != null) && (bmp2 != null) && (bmp3 != null) && (bmp4 != null))
                            {
                                bmp5 = new Bitmap(W, H);
                                g = Graphics.FromImage(bmp5);
                                ms5 = new MemoryStream();

                                Thumbmp = new Bitmap(bmp1, W / 2, H / 2);
                                g.DrawImageUnscaled(Thumbmp, 0, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp2, W / 2, H / 2);
                                g.DrawImageUnscaled(Thumbmp, W / 2, 0);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp3, W / 2, H / 2);
                                g.DrawImageUnscaled(Thumbmp, 0, H / 2);
                                Thumbmp.Dispose();

                                Thumbmp = new Bitmap(bmp4, W / 2, H / 2);
                                g.DrawImageUnscaled(Thumbmp, W / 2, H / 2);
                                Thumbmp.Dispose();

                                bmp5.Save(ms5, ImageFormat.Jpeg);

                                joinedpic = ms5.ToArray();
                                ans = true;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("Worker.JionPic()" + er.Message);
            }
            finally
            {
                if (bmp1 != null) bmp1.Dispose();
                if (bmp2 != null) bmp2.Dispose();
                if (bmp3 != null) bmp3.Dispose();
                if (bmp4 != null) bmp4.Dispose();
                if (bmp5 != null) bmp5.Dispose();
                if (Thumbmp != null) Thumbmp.Dispose();
                if (g != null) g.Dispose();
                if (ms1 != null) { ms1.Close(); ms1.Dispose(); }
                if (ms2 != null) { ms2.Close(); ms2.Dispose(); }
                if (ms3 != null) { ms3.Close(); ms3.Dispose(); }
                if (ms4 != null) { ms4.Close(); ms4.Dispose(); }
                if (ms5 != null) { ms5.Close(); ms5.Dispose(); }
                GC.Collect();
            }
            return ans;
        }

        /// <summary>
        /// 叠加字符
        /// </summary>
        /// <param name="pic">数据</param>
        /// <param name="pos">位置</param>
        /// <param name="printstring">字符</param>
        /// <returns>新数据</returns>
        private static byte[] Print(byte[] pic, Enum_PrintPosition pos, string printstring)
        {
            byte[] ans = null;
            Graphics g = null;
            Image bmp = null;
            MemoryStream ms_in = null;
            MemoryStream ms_out = null;
            try
            {
                if (pic != null)
                {
                    ms_in = new MemoryStream(pic);
                    ms_out = new MemoryStream();
                    bmp = Image.FromStream(ms_in);
                    int x = 0;
                    int y = 0;
                    int w = bmp.Width;
                    int h = cHeight;

                    switch (pos)
                    {
                        case Enum_PrintPosition.Down:
                            y = bmp.Height - cHeight;
                            break;
                        case Enum_PrintPosition.Top:
                            y = 0;
                            break;
                        default:
                            break;
                    }

                    Rectangle rect = new Rectangle(x, y, w, h);
                    g = Graphics.FromImage(bmp);
                    lock (m_backBrish)
                    {
                        g.FillRectangle(m_backBrish, rect);//在图片上画出白色背景
                    }
                    lock (m_txtbrush)
                    {
                        g.DrawString(printstring, m_txtfont, m_txtbrush, rect);//在图片上画出文字
                    }
                    bmp.Save(ms_out, ImageFormat.Jpeg);
                    ans = ms_out.ToArray();
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("Worker.Print()" + er.Message);
            }
            finally
            {
                if (bmp != null) bmp.Dispose();
                if (ms_in != null)
                {
                    ms_in.Close();
                    ms_in.Dispose();
                }
                if (ms_out != null)
                {
                    ms_out.Close();
                    ms_out.Dispose();
                }
                if (g != null) g.Dispose();
                GC.Collect();
            }
            return ans;
        }

        /// <summary>
        /// 添加工作对象
        /// </summary>
        /// <param name="bag"></param>
        public void AddJob(Worker_Bag bag)
        {
            try
            {
                if (bag != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), bag);
                    int n1 = 0;
                    int n2 = 0;
                    ThreadPool.GetAvailableThreads(out n1, out n2);
                    int maxn1 = 2000 - n1;

                    if (maxn1 > G.MaxRunningThreads)
                    {
                        G.MaxRunningThreads = maxn1;
                        if (LogTool.gDebugEnable) LogTool.log.Info("MaxRunningThreads=" + G.MaxRunningThreads.ToString());
                    }
                }
            }
            catch (OutOfMemoryException)
            {
                GC.Collect();
            }
            catch (Exception er)
            {
                LogTool.log.Info("Worker.AddJob()" + er.Message);
            }
        }

    }
}
